package com.adaxi.adc.business.passengertrip.controller;
import com.adaxi.adc.business.passengertrip.feign.service.IPassengerFeignService;
import com.adaxi.adc.business.passengertrip.feign.service.IVehicleTripFeignService;
import com.adaxi.adc.business.passengertrip.pojo.AdcPassenger;
import com.adaxi.adc.business.passengertrip.pojo.AdcPassengerTrip;
import com.adaxi.adc.business.passengertrip.pojo.AdcVehicleTrip;
import com.adaxi.adc.business.passengertrip.pojo.TripDTO;
import com.adaxi.adc.business.passengertrip.service.IPassengerTripService;
import com.adaxi.adc.common.common.CommonResult;
import com.adaxi.adc.common.utils.UuidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Options;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "adxi乘客行程接口文档",tags = "adxi乘客行程接口文档")
@RestController
@EnableTransactionManagement
public class PassengerTripController {
    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @Autowired
    IPassengerTripService passengerTripService;//注入乘客行程service

    @Autowired
    IVehicleTripFeignService iVehicleTripFeignService;//注入feign中service

    @Autowired
    IPassengerFeignService iPassengerFeignService;

    @GetMapping("/trips")
    public CommonResult getTrips(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize
    ){
        try {
            CommonResult<TripDTO> list = iVehicleTripFeignService.getTrips( page, pageSize);
            return CommonResult.SUCCESS("查询全部信息", list);
        }catch (Exception e){
            return CommonResult.FAILURE("失败");
        }
    }

    @ApiOperation("乘客行程关联车辆行程")
    @GetMapping("/passengerTrip/{id}")
    public CommonResult queryById(@PathVariable("id")String id){
        AdcPassengerTrip passengerTrip=passengerTripService.getById(id);
        String id1=passengerTrip.getVehicleTripId();
        CommonResult<AdcVehicleTrip> result=iVehicleTripFeignService.queryById(id1);
        if(result.isSuccess()){
            AdcVehicleTrip vehicleTrip=result.getData();
            passengerTrip.setAdcVehicleTrip(vehicleTrip);
        }
        return CommonResult.SUCCESS("查询成功",passengerTrip);
    }


    @ApiOperation("乘客取消拼车")
    @Transactional
    @DeleteMapping("/passengerTrip/{id}/delete")
    public CommonResult delete(@PathVariable("id")String id){
        AdcPassengerTrip passengerTrip=passengerTripService.getById(id);
        String status=passengerTrip.getStatus();
        if(status.equals("已接受")){
            passengerTripService.removeById(id);
            return CommonResult.SUCCESS("删除乘客行程记录成功");
        }
        return CommonResult.FAILURE("行程已出发，无法取消");
    }


    @ApiOperation("修改车辆行程表中已拼人数")
    @Transactional
    @PutMapping("/passengerTrip/{vehicleTripId}/{numPassengers}/update")
    public CommonResult updateById(@PathVariable("vehicleTripId")String id,
                                   @PathVariable("numPassengers")Integer numToReduce){
        iVehicleTripFeignService.updateById(id,numToReduce);
        return CommonResult.SUCCESS("修改已拼人数成功");
    }



    @ApiOperation("插入乘客行程")
    @PostMapping("/passengerTrip/inster")
    public CommonResult insert(@RequestBody AdcPassengerTrip PassengerTrip){
        PassengerTrip.setId(UuidUtil.get32UUID());
        passengerTripService.save(PassengerTrip);
        String uuid = PassengerTrip.getId();
        return CommonResult.SUCCESS("插入成功",uuid);
    }

    @ApiOperation("依据车辆行程id查询乘客行程")
    @GetMapping("/passengerTripByVehicleTripId/{vehicleTripId}")
    public CommonResult queryByVehicleTripId(@PathVariable("vehicleTripId")String vehicleTripId){
        List<AdcPassengerTrip> adcPassengerTrips = passengerTripService.getByVehicleTripId(vehicleTripId);
        return CommonResult.SUCCESS("查询成功",adcPassengerTrips);
    }


    /**
     * 给司机端远程调用
     * @return
     */
    @ApiOperation("查询全部信息接口")
    @GetMapping("/passengerTrip")
    public CommonResult query(){
        List<AdcPassengerTrip> list=passengerTripService.list();
        return CommonResult.SUCCESS("查询全部信息",list);
    }


    /**
     * 远程调用passenger
     * @param id
     * @return
     */
    @GetMapping("/passenger/query/{id}")
    public CommonResult queryPassengerById(@PathVariable("id")String id){
        CommonResult<AdcPassenger> result=iPassengerFeignService.queryById1(id);
        if(result.isSuccess()){
            return CommonResult.SUCCESS("查询成功",result.getData());
        }
        return CommonResult.FAILURE("远程调用失败");
    }


    @GetMapping("/test")
    public CommonResult test(){
        return CommonResult.SUCCESS("测试成功");
    }


}
